<?php 
	
	/**
	* Room
	*/
	class Room
	{
		public $roomid;       // 
		public $gamename = '假游戏'; 	   // 
		public $players = [];   // 
		public $playernum = 0; //
		public $status = GAME_START;    // 游戏准备阶段
		public $dmid;
		public $opttypes = ["hp","mp"];
		
		// function __construct($roomid="敌人",$gamename="假游戏")
		// {
		// 	# code...
		// 	$this->roomid = $roomid;
		// 	$this->gamename = $gamename;
		// }

		public function enterRoom($player){

			if(isset($this->players[$player->qqid])){
				return false;
			}
			$this->playernum = $this->playernum + 1;
			$player->id = $this->playernum;
			$this->players[$player->qqid] = $player;
			return true;
		}

		public function getPlayerById($id){
			return $this->players[$id];
		}

		public function updatePlayerInfo($player){
			$this->players[$player->qqid]=$player;
		}

		/*格式化输出游戏数据*/
		public function roomformatInfo(){
			$gameinfo = "gamestatus: ".$this->getstatus(true).",   gamename: ".$this->gamename.",   PlayerNum: ".$this->playernum;
			if(!empty($this->round)){ 
				

				$gameinfo .=  "\n" . $this->roundformatInfo();
			}

    		// foreach ($this->players as $key => $value) {
    		// 	$gameinfo .= $value->playerformatInfo() . "\n"; 
    		// }
    		return $gameinfo;

		}
		/*格式化输出回合数据*/
		public function roundformatInfo(){
			$round = $this->round;
			$roundinfo = "roundstatus: ".$this->getroundstatus(true).",   roundnum: ".$round['number'].",   player: ".$round['fromqq'];
			return $roundinfo;
		}
		/*格式化输出玩家数据*/
		public function playerformatInfo(){
			foreach ($this->players as $key => $value) {
    			$gameinfo .= $value->playerformatInfo() . "\n"; 
    		}
    		return $gameinfo;
		}


		public function getstatus($flag = false){
			if($flag){
				return ROOM_STATUS[$this->status];
			}
			return $this->status;
		}
		public function getroundstatus($flag = false){
			$round = $this->round;
			if($flag){
				return ROUND_STATUS[$round['status']];
			}
			return $round['status'];
		}

		/*回合开始*/
		public function roundstart($fromqq){
			$this->status = ROUND_START;
			$this->setRound();
		}
		public function setRound($number = 0,$status = ROUND_RCD,$fromqq = 0){
			$this->round = 
			[
				'number' => $number,
				'status' => $status,
				'fromqq' => $fromqq,
			];
		}
		public function roundend(){
			$round = $this->round;

			$player = $this->players[$round['fromqq']];
			$player->roundend();
			$this->updatePlayerInfo($player);
			$this->setRound($round['number']);
		}	

		public function delcardByRound($ids){
			$player = $this->players[$this->round['fromqq']];

			$ids = explode(",",$ids);
			$cards = $player->handcard;
			foreach ($cards as $key => $value) {
				if(in_array($key, $ids)){
					unset($player->handcard[$key]);
					$player->removecard[$key] = $value;
				}
			}
			$this->updatePlayerInfo($player);
			return $player->handcard;
		}

		public function optsetvalue($opt='')
		{

			//说书人,hp-3,mp+3
			$opt = explode(",", $opt);
			// return $opt;
			if (!is_array($opt)) {
				return false;
			}	

			foreach ($opt as $key => $value) {
				if($key == 0){
					$player = $this->getPlayerByNameOrId($value);
			        if($player === false) return false;
				}else{
					
					$optname = substr($value, 0,2);
					$num = substr($value, 2);
					// return $optname . "," . $num;
					if(in_array($optname,$this->opttypes)){
						$player->$optname = $player->$optname + $num;	
						// return 4;				
					}else{
						return false;
					}
				}
			}
			$this->updatePlayerInfo($player);
			return $player->playerformatInfo();
		}


		/*检测是否是自己回合*/
		public function checkRoundOpt($fromqq){
			$round = $this->round;
			if(!empty($round) && !empty($this->players[$fromqq]) && $round['status'] == ROUND_OPT && $round['fromqq'] == $fromqq){
				return true;
			}else{
				return false;
			}
		}

		/*检测是否可以抽卡*/
		public function checkRoundRcd($fromqq){
			$round = $this->round;
			if(!empty($round) && !empty($this->players[$fromqq]) && $round['status'] == ROUND_RCD && $round['fromqq'] == 0){
				$num = $round['number']+1;

				$this->setRound($num,ROUND_OPT,$fromqq);
				return true;

			}else{
				return false;
			}
		}

		/*检测是否在游戏*/
		public function getPlayerByNameOrId($nameorqq){
			$players = $this->players;

			foreach ($players as $key => $value) {
				if($nameorqq == $key || $value->nickname == $nameorqq){
					return $value;
				}
			}

			if($nameorqq == $this->dmid) return true;
			return false;
		}


	}
?>